package com.bdqn.service.impl;

import com.bdqn.mapper.MenuMapper;
import com.bdqn.pojo.Menu;
import com.bdqn.service.MenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    MenuMapper menuMapper;

    @Override
    public List<Menu> getUserMenus(int uid) {
        List<Menu> list=new ArrayList<>();
        //获取当前用户的所有父菜单
        list=menuMapper.selectParentMenuByUid(uid);
        //根据父菜单编号，查询用户父菜单下所有的子菜单
        for (Menu pmenu:list) {
            //获取父菜单编号
            int pid=pmenu.getMenuId();
            //查询用户父菜单下所有的子菜单
            List<Menu> childeMenuList=menuMapper.selectChildMenusByUidAndPid(uid,pid);
            //放到父菜单下
            pmenu.setChildMenuList(childeMenuList);
        }
        return list;
    }

    @Override
    public PageInfo<Menu> selectPage(int pageNum, int pageSize, String name) {
        //开始分页,设置当前页的页号和每页显示的条数
        PageHelper.startPage(pageNum,pageSize);
        //获取所有的菜单信息
        List<Menu> list=menuMapper.selectByName(name);
        //获取当前页的信息对象
        PageInfo<Menu> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Menu> getMenusAll() {
        List<Menu> list=new ArrayList<>();
        //获取所有父菜单
        list=menuMapper.selectParentMenus();
        //根据父菜单编号，查询父菜单下所有的子菜单
        for (Menu pmenu:list) {
            //获取父菜单编号
            int pid=pmenu.getMenuId();
            //查询父菜单下所有的子菜单
            List<Menu> childeMenuList=menuMapper.selectChildMenusByPid(pid);
            //放到父菜单下
            pmenu.setChildMenuList(childeMenuList);
        }
        return list;
    }


}
